package com.yingxin.yx.framework.web;

import com.yingxin.yx.framework.commons.helper.JacksonHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author fanchao
 */
public class ResultUtil {


    public static void responseJson(HttpServletRequest request, HttpServletResponse response,Object obj) {
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        servletResponse.setStatus(HttpServletResponse.SC_OK);
        servletResponse.setContentType("application/json");
        servletResponse.setCharacterEncoding("UTF-8");
        servletResponse.setHeader("Access-Control-Allow-Credentials", "true");
        servletResponse.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        try {
            servletResponse.getWriter().write(JacksonHelper.toJson(obj));
        } catch (IOException e1) {
            // log.error(e1.getMessage(), e1);
        }
    }
}
